package com.snail.controller;

import com.snail.model.pojo.mall.BizOrderDetail;
import com.snail.result.Result;
import com.snail.service.BizOrderDetailService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/orderDetail")
public class BizOrderDetailController {

    @Autowired
    private BizOrderDetailService orderDetailService;

    // 新增订单明细
    @PostMapping("add")
    public Result<String> addDetail(@RequestBody BizOrderDetail detail) {
        boolean saved = orderDetailService.save(detail);
        return saved ? Result.success("添加成功") : Result.failed("添加失败");
    }

    // 批量新增
    @PostMapping("/batch")
    public Result<String> addDetails(@RequestBody List<BizOrderDetail> details) {
        boolean saved = orderDetailService.batchInsertDetails(details);
        return saved ? Result.success("批量添加成功") : Result.failed("批量添加失败");
    }

    // 删除订单明细
    @DeleteMapping("/deletebyid/{id}")
    public Result<String> deleteDetail(@PathVariable Long id) {
        boolean removed = orderDetailService.removeById(id);
        return removed ? Result.success("删除成功") : Result.failed("删除失败");
    }

    // 修改订单明细
    @PutMapping("update")
    public Result<String> updateDetail(@RequestBody BizOrderDetail detail) {
        boolean updated = orderDetailService.updateById(detail);
        return updated ? Result.success("修改成功") : Result.failed("修改失败");
    }

    // 根据订单号查询明细
    @GetMapping("/byOrder/{orderNo}")
    public Result<List<BizOrderDetail>> getByOrderNo(@PathVariable String orderNo) {
        List<BizOrderDetail> details = orderDetailService.getDetailsByOrderNo(orderNo);
        System.out.println(orderNo);
        return Result.success(details);
    }

    // 获取单个明细
    @GetMapping("/searchbyid/{id}")
    public Result<BizOrderDetail> getDetail(@PathVariable Long id) {
        BizOrderDetail detail = orderDetailService.getById(id);
        return detail != null ? Result.success(detail) : Result.failed("未找到记录");
    }
}